package ru.papergames.view.mediators
{
	import mx.utils.URLUtil;
	
	import org.puremvc.as3.interfaces.INotification;
	
	import ru.papergames.view.mediators.BaseMediator;
	import ru.papergames.controller.notifications.FrontendNotifications;
	import ru.papergames.model.proxies.RegistrationProxy;
	import ru.papergames.view.screens.RegistrationTopView;
	import ru.papergames.view.events.RegistartionEvent;
	import ru.papergames.utils.constants.ConfigKeys;

	public class RegistrationTopViewMediator extends BaseMediator
	{
		
		public static const NAME:String = 'RegistrationTopViewMediator';
		
		public function RegistrationTopViewMediator(viewComponent:Object=null)
		{
			super(NAME, viewComponent);
		}
		
		override public function onRegister():void {
			super.onRegister();
			
			//captcha url
			var captcha_url:String = URLUtil.getFullURL(this.settingsProxy.rootURL, this.settingsProxy.getProperty(ConfigKeys.CAPTCHA_URL));							   
			view.secretCode.load(captcha_url + '?JSESSIONID=' + this.settingsProxy.httpSession);
			
			addListeners();
		}
		
		override public function onRemove():void {
			super.onRemove();
			removeListeners();
			
			//clean fields
			view.captcha_code.text = '';
			view.regPanel.status = '';
			
			this.setViewComponent(null);
		}
		
		protected function get view():RegistrationTopView {
			return this.viewComponent as RegistrationTopView;
		}
		
		protected function get regProxy():RegistrationProxy {
			return this.getProxy(RegistrationProxy.NAME) as RegistrationProxy;
		}
		
		override public function listNotificationInterests():Array {
			var notes:Array = super.listNotificationInterests();
			
			notes.push( RegistrationProxy.TRY_REGISTER_SUCCESS,
						RegistrationProxy.TRY_REGISTER_FAULT);
			
			return notes;
		}
		
		override public function handleNotification(note:INotification):void {
			var captchaToReload:Boolean = false;
			
			
			if (!view) return; 				
				
			view.tryRegButton.enabled = true;
			view.cancelButton.enabled = true;
			
			
			switch(note.getName()) {				
				case RegistrationProxy.TRY_REGISTER_SUCCESS: 
					view.regPanel.status = 'Регистрация удалась';
					break;					
				
				case RegistrationProxy.TRY_REGISTER_FAULT: 
					view.regPanel.status = 'Ошибка регистрации';
					captchaToReload = true;
					break;
					
			}
			
			
			
			if (view && captchaToReload) view.secretCode.load();
			
		}
		
		
		/*** PRIVATE IMPLEMENTATION ********/
		
		private function addListeners():void {
			view.addEventListener(RegistartionEvent.TRY_REGISTER, onTryReg);
			view.addEventListener(RegistartionEvent.CANCEL_REGISTER, onCancelReg);
		}
		
		private function removeListeners():void {
			view.removeEventListener(RegistartionEvent.TRY_REGISTER, onTryReg);
			view.removeEventListener(RegistartionEvent.CANCEL_REGISTER, onCancelReg);
		}		
		
		private function onTryReg(evt:RegistartionEvent):void {
			view.regPanel.status = '';
			view.tryRegButton.enabled = false;
			view.cancelButton.enabled = false;
			
			facade.sendNotification(FrontendNotifications.TRY_TO_REGISTER, evt.registrationVO);
		}
		
		private function onCancelReg(evt:RegistartionEvent):void {
			facade.sendNotification(FrontendNotifications.CANCEL_REGISTRATION);
		}
		
	}
}